package com.example.tests;

import static com.example.fw.RandomizeHelper.getRandomNumber;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class GroupModificationTests extends TestBase
{
	@Override
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator()
	{
		return super.randomValidGroupGenerator();
	}

	@Test(dataProvider = "randomValidGroupGenerator")
	public void modifySomeGroup(GroupData group)
	{
		SortedListOf<GroupData> oldList = app.getModel().getGroups();
		if (oldList.size() > 0)
		{
			int index = getRandomNumber(0, oldList.size() - 1);

			app.getGroupHelper().modifyGoup(index, group);

			SortedListOf<GroupData> newList = app.getModel().getGroups();

			assertThat(newList, equalTo(oldList.without(index).withAdded(group)));

			if (timeToCheck())
			{
				if ("yes".equals(app.getProperty("check.db")))
				{
					assertThat(newList, equalTo(app.getHibernateHelper().getGroups()));
				}
				if ("yes".equals(app.getProperty("check.ui")))
				{
					assertThat(newList, equalTo(app.getGroupHelper().getUIGroups()));
				}
			}
		}
	}
}
